package com.meixiang.activity;

import android.app.Activity;
import android.content.Intent;
import android.net.http.SslError;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.meixiang.R;
import com.meixiang.activity.account.login.LoginActivity;
import com.meixiang.global.ContentHint;
import com.meixiang.http.HttpUtils;
import com.meixiang.main.BaseActivity;
import com.meixiang.main.MXApplication;
import com.meixiang.util.AbToastUtil;
import com.meixiang.view.ProgressWebView;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;

public class WebviewActivity extends BaseActivity {
    @Bind(R.id.webView)
    ProgressWebView webView;
    private String title;
    private String url;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_webview);
        ButterKnife.bind(this);
        title = getIntent().getStringExtra("title");
        url = getIntent().getStringExtra("url");
        super.onCreate(savedInstanceState);
    }


    @Override
    protected void initTitle() {
        //setTitle(title, true);
        setTitle(title, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (webView.canGoBack()) {
                    webView.goBack();
                }else {
                    finish();
                }
            }
        });
    }

    @Override
    protected void initView() {
        webView. setWebViewClient(new WebViewClient() {

            @SuppressWarnings("deprecation")
            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                view.loadUrl("file:///android_asset/404.html"); //加载出错页面
            }

            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                //handler.cancel(); // Android默认的处理方式
                handler.proceed();  // 接受所有网站的证书  授权证书()
                //handleMessage(Message msg); // 进行其他处理
            }
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (url.contains("sessionid")) {
                    String sessionId = url.substring(url.lastIndexOf("=") + 1);
                    HashMap<String, String> vals = new HashMap<String, String>();
                    vals.put("fundSessionId", sessionId);
                    MXApplication.mApp.getSession().set(vals);
                    Intent mIntent = new Intent();
                    // 设置结果，并进行传送
                    setResult(Activity.RESULT_OK, mIntent);
                    finish();
                }else{
                    view.loadUrl(url);
                }
                return true;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                if (url.contains("http://meixianginsurance/?reason=1&target=login")){
                    context.startActivity(new Intent(context,LoginActivity.class));
                    AbToastUtil.showToast(context, ContentHint.FIRST_LOGIN);
                    MXApplication.mApp.logout();
                    finish();
                }
            }
        });
//        webView.loadUrl(url,getHeader());
        webView.loadUrl(url, HttpUtils.getHeader().headersMap);
    }

    @Override
    protected void setData() {

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK&& webView.canGoBack()) {
            webView.goBack();
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onClick(View v) {

    }
}
